layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 调度单表管理
     */
    var TransportDispatch = {
        tableId: "transportDispatchTable"
    };

    /**
     * 初始化表格的列
     */
    TransportDispatch.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'dispatchNumber', sort: true, title: '单号', style:'color: #2b85e4;font-weight: 600;',width:260,
            	templet: function(d){
            		var str = "";
            		if (d.customerOrder) {
						str += "单号：" + d.customerOrder + '<br/>';
					}
            		if (d.undertime) {
						str += "发货日期：" + d.setMentiontime + '<br/>';
					}
            		if (d.undertime) {
						str += "线路：" + d.lineName + '<br/>';
					}
            		if (d.kilometer) {
						str += "公里数：" + d.kilometer;
					}
            		return str
            	}
			},
            {field: 'carnumbger', sort: true, title: '车辆',width:220,
            	templet: function(d){
            		var str = "";
            		if (d.carnumbger) {
						str += "车牌号：" + d.carnumbger + '<br/>';
					}
            		if (d.driver1) {
						str += "司机：" + d.driver1 + '<br/>';
					}
            		if (d.cartel) {
						str += "电话：" + d.cartel;
					}
            		return str
            	}
			},

            {field: 'getUnit', title: '卸货单位', style:'background-color: #dbf1e1;font-weight: 600;',width:300,
            	templet: function(d){
            		var str = "";
            		if (d.getUnit) {
						str += "卸货单位：" + d.getUnit + '<br/>';
					}
            		if (d.getLocation) {
						str += "地址：" + d.getLocation + '<br/>';
					}
            		if (d.getPeople) {
						str += "联系人：" + d.getPeople + '<br/>';
					}
            		if (d.getTel) {
						str += "电话：" + d.getTel;
					}
            		return str
            	}
			},

            {field: 'goodsName', title: '商品',width:260,
            	templet: function(d){
            		var str = "";
            		if (d.goodsName) {
						str += "商品：" + d.goodsName + '<br/>';
					}
            		if (d.goodsNumber) {
						str += "编号：" + d.goodsNumber + '<br/>';
					}
            		if (d.goodsNo) {
						str += "货号：" + d.goodsNo + '<br/>';
					}
            		if (d.goodsNo) {
						str += "批次：" + d.batchnumber;
					}
            		return str
            	}
			},
			
            {field: 'tooDeployVolume', title: '体积',width:260,
            	templet: function(d){
            		var str = "";
            		if (d.tooDeployVolume) {
						str += "体积：" + d.tooDeployVolume + '<br/>';
					}
            		if (d.transportMoney) {
						str += "金额：" + d.transportMoney + '<br/>';
					}
            		if (d.tooDeployNum) {
						str += "数量：" + d.tooDeployNum;
					}
            		return str
            	}
			},

            {field: 'goodsPacking', title: '包装',width:260,
            	templet: function(d){
            		var str = "";
            		if (d.goodsPacking) {
						str += "包装：" + d.goodsPacking + '<br/>';
					}
            		if (d.goodsNumber) {
						str += "规格：" + d.goodsSpecs + '<br/>';
					}
            		return str
            	}
			},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportDispatch.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['customerOrder'] = $("#customerOrder").val();
        table.reload(TransportDispatch.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    TransportDispatch.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/transportDispatch/add';
    };

    /**
     * 导出excel按钮
     */
    TransportDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TransportDispatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TransportDispatch.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/transportDispatch/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportDispatch.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportDispatch/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportDispatch.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportDispatch.tableId,
        url: Feng.ctxPath + '/transportDispatch/list?dispatchNumber='+$("#dispatchNumber").val()+'&getUnitid='+$("#getUnitid").val()+'&getLocationid='+$("#getLocationid").val(),
        page: true,
        limit: 30,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportDispatch.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportDispatch.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TransportDispatch.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TransportDispatch.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TransportDispatch.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TransportDispatch.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TransportDispatch.onDeleteItem(data);
        }
    });
});
